package com.ape.mapsa.produccion.service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ape.mapsa.bpm.service.IBPMService;
import com.ape.mapsa.produccion.dao.AssignmentDAO;
import com.ape.mapsa.produccion.dao.ClienteDAO;
import com.ape.mapsa.produccion.dao.ColorDAO;
import com.ape.mapsa.produccion.dao.ConfeccionDAO;
import com.ape.mapsa.produccion.dao.DepItemPrintSpecsDAO;
import com.ape.mapsa.produccion.dao.EstadoProduccionDAO;
import com.ape.mapsa.produccion.dao.ExtrusionDAO;
import com.ape.mapsa.produccion.dao.FormulaMpDAO;
import com.ape.mapsa.produccion.dao.FormulasDAO;
import com.ape.mapsa.produccion.dao.ImpresionDAO;
import com.ape.mapsa.produccion.dao.ItemDAO;
import com.ape.mapsa.produccion.dao.ItemTipoDAO;
import com.ape.mapsa.produccion.dao.MaquinaDAO;
import com.ape.mapsa.produccion.dao.MaterialsDAO;
import com.ape.mapsa.produccion.dao.MuestraDAO;
import com.ape.mapsa.produccion.dao.OpCommentDAO;
import com.ape.mapsa.produccion.dao.OpDAO;
import com.ape.mapsa.produccion.dao.OperatorDAO;
import com.ape.mapsa.produccion.dao.PedidoVentaDAO;
import com.ape.mapsa.produccion.dao.PerformedWorkDAO;
import com.ape.mapsa.produccion.dao.ProdScrapDAO;
import com.ape.mapsa.produccion.dao.ProdStateDAO;
import com.ape.mapsa.produccion.model.Cliente;
import com.ape.mapsa.produccion.model.Color;
import com.ape.mapsa.produccion.model.ConfPerformedWork;
import com.ape.mapsa.produccion.model.ConfProdScrap;
import com.ape.mapsa.produccion.model.Confeccion;
import com.ape.mapsa.produccion.model.DepItemPrintSpec;
import com.ape.mapsa.produccion.model.EstadoProduccion;
import com.ape.mapsa.produccion.model.ExtPerformedWork;
import com.ape.mapsa.produccion.model.ExtProdScrap;
import com.ape.mapsa.produccion.model.Extrusion;
import com.ape.mapsa.produccion.model.Formula;
import com.ape.mapsa.produccion.model.FormulaMp;
import com.ape.mapsa.produccion.model.ImpPerformedWork;
import com.ape.mapsa.produccion.model.ImpProdScrap;
import com.ape.mapsa.produccion.model.Impresion;
import com.ape.mapsa.produccion.model.InvRawMaterialsV;
import com.ape.mapsa.produccion.model.Item;
import com.ape.mapsa.produccion.model.ItemTipo;
import com.ape.mapsa.produccion.model.Maquina;
import com.ape.mapsa.produccion.model.Muestra;
import com.ape.mapsa.produccion.model.OpComment;
import com.ape.mapsa.produccion.model.OrdenProduccion;
import com.ape.mapsa.produccion.model.PedidoVenta;
import com.ape.mapsa.produccion.model.ProdOperator;

@Service
@Transactional
public class ProduccionService implements IProduccionService, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3162186848709318637L;

	private final static Logger log = Logger.getLogger(ProduccionService.class);

	@Autowired
	ItemDAO itemDAO;

	@Autowired
	OperatorDAO operatorDAO;

	@Autowired
	MaterialsDAO mpDAO;

	@Autowired
	ProdScrapDAO scrapDAO;

	@Autowired
	ExtrusionDAO extrusionDao;
	@Autowired
	ConfeccionDAO confeccionDao;
	@Autowired
	ImpresionDAO impresionDao;

	@Autowired
	FormulaMpDAO formulaMpDAO;

	@Autowired
	FormulasDAO formulasDAO;

	@Autowired
	DepItemPrintSpecsDAO depItemPrintSpecDAO;

	@Autowired
	OpDAO prodDao;

	@Autowired
	PerformedWorkDAO performedWorkDao;

	@Autowired
	ItemTipoDAO itemTipoDao;

	@Autowired
	ColorDAO colorDao;

	@Autowired
	MuestraDAO muestraDao;

	@Autowired
	ClienteDAO clienteDao;

	@Autowired
	ProdStateDAO prodStateDao;

	@Autowired
	PedidoVentaDAO pedidoVentaDao;

	@Autowired
	EstadoProduccionDAO estadoProduccionDAO;

	@Autowired
	OpCommentDAO opCommentDAO;

	@Autowired
	MaquinaDAO maqDAO;

	@Autowired
	AssignmentDAO maqAssignDAO;

	@Autowired
	IBPMService bpmService;

	/**
	 * Add User
	 * 
	 * @param User
	 *            user
	 */
	// @Transactional(readOnly = false)

	@Override
	public List<Item> getAllItems() {
		return itemDAO.getAll();
	}

	@Override
	@Transactional(readOnly = false)
	public void addOp(OrdenProduccion orden, String author, Long userID) {
		try {
			log.debug(orden.getItem());
			float grm2 = orden.getItem().getEspesor() * orden.getItem().getDensidad();
			float ancho = orden.getItem().getAncho() * 0.02F;
			float gramaje = ancho * grm2;
			float largo = orden.getItem().getLargo() / 100;
			float pesoBolsa = gramaje * largo;
			Extrusion ext = new Extrusion();
			ext.setGrm2(grm2);
			ext.setPesoBolsa(pesoBolsa);
			ext.setGramaje(gramaje);
			ext.setOrdenProduccion(orden);
			ext.setCreatedBy(author);
			ext.setCreationDate(new Date());
			ext.setTotalBobinas(0);
			ext.setTotalKilos(0F);
			ext.setTotalMts(0F);
			ext.setTotalScrap(0F);

			log.debug("DATOS EN ORDEN: " + grm2 + "" + ancho + "" + gramaje + "" + largo + "" + pesoBolsa);
			log.debug("Extrusion: " + ext);
			orden.setExtrusion(ext);

			if (orden.getItem().getDepItemPrintSpec() != null && orden.getItem().getDepItemPrintSpec().getId() != null) {
				Impresion imp = new Impresion();
				imp.setCreatedBy(author);
				imp.setCreationDate(new Date());
				imp.setOrdenProduccion(orden);

				imp.setTotalBobinas(0);
				imp.setTotalKilos(0F);
				imp.setTotalMts(0F);
				imp.setTotalScrap(0F);

				orden.setImpresions(imp);
			}

			Confeccion conf = new Confeccion();
			conf.setCreatedBy(author);
			conf.setCreationDate(new Date());
			conf.setOrdenProduccion(orden);
			conf.setTotalBobinas(0);
			conf.setTotalBolsas(0);
			conf.setTotalBultos(0);
			conf.setTotalScrap(0F);
			orden.setConfeccions(conf);

			prodDao.save(orden);
			bpmService.startProductionProcess(orden.getPedidoVenta().getId(), orden.getId(), orden.getItem().getCodigo(), author, orden.getItem()
					.getItemState());
		} catch (Exception x) {
			// prodDao.delete(orden);
			x.printStackTrace();
			throw x;
		}
	}

	@Override
	public List<OrdenProduccion> getAllOps() {
		return prodDao.getAll();
	}

	public List<ItemTipo> getAllItemTipos() {
		return itemTipoDao.getAll();
	}

	public List<Color> getAllColors() {
		return colorDao.getAll();
	}

	public Color getColorByDescription(Color color) {
		return colorDao.findByDescripcion(color);
	}

	@Override
	public EstadoProduccion getProdStateById(Long id) {
		return prodStateDao.getById(id);
	}

	public List<Muestra> getAllMuestras() {
		return muestraDao.getAll();
	}

	@Transactional(readOnly = false)
	public void saveMuestra(Muestra muestra) {
		muestraDao.save(muestra);
	}

	@Override
	public PedidoVenta getPedidoById(Long pedidoVenta) {
		return pedidoVentaDao.getById(pedidoVenta);
	}

	@Override
	@Transactional(readOnly = false)
	public PedidoVenta savePedidoVenta(PedidoVenta pedidoVenta) {
		return pedidoVentaDao.save(pedidoVenta);
	}

	public List<Cliente> getAllClientes() {
		return clienteDao.getAll();
	}

	@Transactional(readOnly = false)
	public void saveCliente(Cliente newCliente) {
		newCliente.setState(1);
		newCliente.setCreationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newCliente.setCreatedby("" + request.getSession().getAttribute("email"));
		clienteDao.save(newCliente);
	}

	@Override
	public PedidoVenta getPedidoByNumero(PedidoVenta pedidoVenta) {
		return pedidoVentaDao.getPedidoByNumero(pedidoVenta);
	}

	@Transactional(readOnly = false)
	public void saveColor(Color newColor) {
		colorDao.save(newColor);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateOp(OrdenProduccion newOrden) {
		newOrden.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newOrden.setModifiedby("" + request.getSession().getAttribute("email"));
		prodDao.update(newOrden);
	}

	public List<InvRawMaterialsV> getAllMp() {
		return mpDAO.getAll();
	}

	@Transactional(readOnly = false)
	public void saveItem(Item newItem) {
		newItem.setItemCategoria("PT");
		itemDAO.save(newItem);
	}

	@Transactional(readOnly = false)
	public void updateItem(Item newItem) {
		itemDAO.update(newItem);
	}

	public Item getItemById(Long id) {
		return itemDAO.getById(id);
	}

	@Override
	public EstadoProduccion getEstadoProduccionById(Long consequence) {
		return estadoProduccionDAO.getById(consequence);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateOrdenProd(OrdenProduccion selectedOp) {
		selectedOp.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		selectedOp.setModifiedby("" + request.getSession().getAttribute("email"));
		prodDao.update(selectedOp);
	}

	@Override
	@Transactional(readOnly = false)
	public void saveComment(OpComment comment) {
		opCommentDAO.save(comment);
	}

	@Override
	public List<OrdenProduccion> getOrdenProdByState(Long l) {
		return prodDao.getAllByState(l);
	}

	@Override
	public Integer countOrdenProdByState(Long l) {
		return prodDao.countOrdenProdByState(l);
	}

	@Override
	public Item getItemByCode(String itemCode) {
		return itemDAO.getByCode(itemCode);
	}

	@Override
	public OrdenProduccion getOpById(long opId) {
		return prodDao.getById(opId);
	}

	@Override
	public void saveDepItemPrintSpces(DepItemPrintSpec depItemPrintSpecs) {
		depItemPrintSpecDAO.save(depItemPrintSpecs);
	}

	@Override
	public Long getPedidVentaNextId() {
		return pedidoVentaDao.getPedidoNextId();
	}

	public List<Maquina> getMaquinas() {
		return maqDAO.getAll();
	}

	public void addMaquina(Maquina maq) {
		maqDAO.save(maq);
	}

	public void updateMaquina(Maquina maq) {
		maqDAO.update(maq);
	}

	public void delteMaquina(Maquina maq) {
		maqDAO.deleteMaq(maq);
	}

	public Maquina getMaquinaByCode(Maquina maq) {
		return maqDAO.getByCode(maq.getMaqCodigo());
	}

	public Maquina getmaMaquinaById(Maquina maq) {
		return maqDAO.getById(maq.getId());
	}

	@Override
	public List<Maquina> findMaquinaTypeByCode(String query) {
		return maqDAO.searchMaquinaByCode(query);
	}

	@Override
	public List<Maquina> getMaquinasByType(Integer valor) {
		return maqDAO.getMaquinasByType(valor);
	}

	public List<Extrusion> getMaqAssginByType(Integer tipoMaquina) {
		return maqAssignDAO.getAssginmentsByType(tipoMaquina);
	}

	@Override
	public List<Extrusion> getAssignationByMaquina(Long id) {
		return maqAssignDAO.getByMaquinaId(id);
	}

	@Override
	public List<Impresion> getImpAssginByType(Integer valor) {
		return maqAssignDAO.getImpAssginments(valor);
	}

	@Override
	public List<Confeccion> getConfAssginByType(Integer valor) {
		return maqAssignDAO.getConfAssginments(valor);
	}

	@Override
	public void saveExtrusionPerformedWork(ExtPerformedWork extPerformedWork) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		extPerformedWork.setCreatedby("" + request.getSession().getAttribute("email"));
		extPerformedWork.setState(1);
		extPerformedWork.setCreationdate(new Date());
		performedWorkDao.saveExtPerformedWork(extPerformedWork);
	}

	@Override
	public void updateExtrusionPerformedWork(ExtPerformedWork extPerformedWork) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		extPerformedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		extPerformedWork.setModificationdate(new Date());
		performedWorkDao.updateExtPerformedWork(extPerformedWork);
	}

	@Override
	public void deleteExtrusionPerformedWork(ExtPerformedWork extPerformedWork) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		extPerformedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		extPerformedWork.setModificationdate(new Date());
		extPerformedWork.setState(0);
		performedWorkDao.updateExtPerformedWork(extPerformedWork);
	}

	@Override
	public List<Formula> getFormulas(String query) {
		return formulasDAO.getFromulas(query);
	}

	@Override
	public List<Formula> getAllFormulas() {
		return formulasDAO.getAllFormulas();
	}

	@Override
	public List<Item> getMpByQuery(String query) {
		return mpDAO.getByQuery(query);
	}

	@Override
	public void saveFormula(Formula selectedFormula) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		selectedFormula.setCreatedby("" + request.getSession().getAttribute("email"));
		selectedFormula.setCreationDate(new Date());
		formulasDAO.save(selectedFormula);
	}

	@Override
	public void saveFormulaMp(FormulaMp formulaMp) {
		formulaMpDAO.save(formulaMp);
	}

	@Override
	public void updateFormula(Formula selectedFormula) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		selectedFormula.setModificationDate(new Date());
		selectedFormula.setModifiedby("" + request.getSession().getAttribute("email"));
		formulasDAO.update(selectedFormula);
	}

	@Override
	public Formula getFormulaById(Long id) {
		return formulasDAO.getById(id);
	}

	@Override
	public void updateFormulaMp(FormulaMp selectedMps) {
		formulaMpDAO.update(selectedMps);
	}

	@Override
	public void deleteFormulaMp(FormulaMp originalFmp) {
		formulaMpDAO.delete(originalFmp);
	}

	@Override
	public List<FormulaMp> getMpForFormula(Formula id) {
		return formulaMpDAO.getByFormulaId(id);
	}

	@Override
	public Formula getFormulaByDescription(String description) {
		return formulasDAO.getFormulaByDescription(description);
	}

	@Override
	public void saveProdExtrusionScrap(ExtProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setCreatedby("" + request.getSession().getAttribute("email"));
		scrap.setState(1);
		scrapDAO.saveExtScrap(scrap);
	}

	@Override
	public void updateProdExtrusionScrap(ExtProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrap.setModificationdate(new Date());
		scrapDAO.updateExtScrap(scrap);
	}

	@Override
	public void deleteProdExtrusionScrap(ExtProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrap.setModificationdate(new Date());
		scrap.setState(0);
		scrapDAO.updateExtScrap(scrap);
	}

	@Override
	public void updateColor(Color newColor) {
		colorDao.update(newColor);
	}

	@Override
	public void deleteColor(Color newColor) {
		colorDao.delete(newColor);
	}

	@Override
	public void updateClient(Cliente newCliente) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newCliente.setModifiedby("" + request.getSession().getAttribute("email"));
		newCliente.setModificationdate(new Date());
		clienteDao.update(newCliente);
	}

	@Override
	public void deleteCliente(Cliente newCliente) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newCliente.setState(0);
		newCliente.setModifiedby("" + request.getSession().getAttribute("email"));
		newCliente.setModificationdate(new Date());
		updateClient(newCliente);
	}

	@Override
	public List<ExtPerformedWork> getExtrusionPerformedWorks(Extrusion extrusion) {
		return performedWorkDao.getExtrusionPerformedWork(extrusion);
	}

	@Override
	public List<ExtProdScrap> getExtrusionScraps(Extrusion extrusion) {
		return scrapDAO.getExtrusionScrap(extrusion);
	}

	@Override
	public List<ConfProdScrap> getConfectionScraps(Confeccion confeccion) {
		return scrapDAO.getConfeccionScrap(confeccion);
	}

	@Override
	public List<ImpPerformedWork> getImpresionPerformedWorks(Impresion impresion) {
		return performedWorkDao.getImpPerformedWork(impresion);
	}

	@Override
	public List<ImpProdScrap> getImpresionScraps(Impresion impresion) {
		return scrapDAO.getImpresionScrap(impresion);
	}

	@Override
	public List<ConfPerformedWork> getConfectionPerformedWorks(Confeccion confeccion) {
		return performedWorkDao.getConfPerformedWork(confeccion);
	}

	@Override
	public void updateExtrusion(Extrusion extrusion) {
		extrusion.setModificationDate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		extrusion.setModifiedBy("" + request.getSession().getAttribute("email"));
		extrusionDao.update(extrusion);
	}

	@Override
	public void saveImpresionPerformedWork(ImpPerformedWork impPerformedWork) {
		impPerformedWork.setCreationdate(new Date());
		impPerformedWork.setState(1);
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		impPerformedWork.setCreatedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.saveImpPerformedWork(impPerformedWork);
	}

	@Override
	public void saveConfeccionPerformedWork(ConfPerformedWork confPerformedWork) {
		confPerformedWork.setCreationdate(new Date());
		confPerformedWork.setState(1);
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		confPerformedWork.setCreatedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.saveConfPerformedWork(confPerformedWork);
	}

	@Override
	public void updateImpresionPerformedWork(ImpPerformedWork performedWork) {
		performedWork.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		performedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.updateImpPerformedWork(performedWork);
	}

	@Override
	public void updateConfeccionPerformedWork(ConfPerformedWork performedWork) {
		performedWork.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		performedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.updateConfPerformedWork(performedWork);

	}

	@Override
	public void deleteImpresionPerformedWork(ImpPerformedWork performedWork) {
		performedWork.setState(0);
		performedWork.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		performedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.updateImpPerformedWork(performedWork);
	}

	@Override
	public void deleteConfeccionPerformedWork(ConfPerformedWork performedWork) {
		performedWork.setState(0);
		performedWork.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		performedWork.setModifiedby("" + request.getSession().getAttribute("email"));
		performedWorkDao.updateConfPerformedWork(performedWork);
	}

	@Override
	public void deleteProdImpresionScrap(ImpProdScrap scrap) {
		scrap.setState(0);
		scrap.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrapDAO.updateImpScrap(scrap);
	}

	@Override
	public void deleteProdConfeccionScrap(ConfProdScrap scrap) {
		scrap.setState(0);
		scrap.setModificationdate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrapDAO.updateConfScrap(scrap);

	}

	@Override
	public void updateImpresion(Impresion impresions) {
		impresions.setModificationDate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		impresions.setModifiedBy("" + request.getSession().getAttribute("email"));
		impresionDao.update(impresions);
	}

	@Override
	public void updateConfeccion(Confeccion confeccion) {
		confeccion.setModificationDate(new Date());
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		confeccion.setModifiedBy("" + request.getSession().getAttribute("email"));
		confeccionDao.update(confeccion);
	}

	@Override
	public void saveProdConfeccionScrap(ConfProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setCreatedby("" + request.getSession().getAttribute("email"));
		scrap.setState(1);
		scrapDAO.saveConfScrap(scrap);
	}

	@Override
	public void saveProdImpresionScrap(ImpProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setCreatedby("" + request.getSession().getAttribute("email"));
		scrap.setState(1);
		scrapDAO.saveImpScrap(scrap);

	}

	@Override
	public void updateProdImpresionScrap(ImpProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrap.setModificationdate(new Date());
		scrapDAO.updateImpScrap(scrap);
	}

	@Override
	public void updateProdConfeccionScrap(ConfProdScrap scrap) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		scrap.setModifiedby("" + request.getSession().getAttribute("email"));
		scrap.setModificationdate(new Date());
		scrapDAO.updateConfScrap(scrap);
	}

	@Override
	public List<ProdOperator> getAllOperators() {
		return operatorDAO.getAllOperators();
	}

	@Override
	public void updateOperator(ProdOperator newOperator) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newOperator.setModificationdate(new Date());
		newOperator.setModifiedby("" + request.getSession().getAttribute("email"));
		operatorDAO.update(newOperator);
	}

	@Override
	public void saveOperator(ProdOperator newOperator) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		newOperator.setCreationdate(new Date());
		newOperator.setCreatedby("" + request.getSession().getAttribute("email"));
		operatorDAO.save(newOperator);
	}

	@Override
	public List<ProdOperator> getOperators(String query) {
		return operatorDAO.getOperators(query);
	}
}
